package com.mvp.shq.washer.view.widget;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.graphics.Region.Op;
import android.graphics.Path;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.mvp.shq.washer.APP;
import com.mvp.shq.washer.R;
import com.zhy.autolayout.utils.L;

import static com.umeng.socialize.utils.DeviceConfig.context;

/**
 * 水流波动控件
 *
 * @author chenjing
 *
 */
public class WaveView extends View
{
    private final String target = "waveview";

    private int len;
    private RectF oval;
    private Paint paint;
    private float[] firstWaterLine;
    private float[] secondWaterLine;
    private int move = 0;
    //如果在运行，就不会执行下次动画
    private boolean isRunning = false;
    //判断是上升还是下降
    public int state = 1;
    public int up;

    private boolean isMeasured = false;

    public WaveView(Context context)
    {
        super(context);
        init();
    }

    public WaveView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init();
    }

    public WaveView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        init();
    }

    private void init()
    {
        moveWaterLine();

    }

    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus)
    {
        super.onWindowFocusChanged(hasWindowFocus);
        // 开始波动

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        len = Math.min(width,height);
        //实例化矩形
        oval = new RectF(0,0,len,len);
        //定义两个数组，保存Y值
        firstWaterLine = new float[len];
        secondWaterLine = new float[len];
        //设置测量高度和宽度
        setMeasuredDimension(len,len);
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        paint =new Paint();
        //设置画笔颜色
        paint.setColor(Color.WHITE);
        //设置画笔抗锯齿
        paint.setAntiAlias(true);
        //让画出的图形是空心的(不填充)
        paint.setStyle(Paint.Style.STROKE);
//        Bitmap bitmap = BitmapFactory.decodeResource(APP.getAppResources(),R.mipmap.home_incircle);
//        canvas.drawBitmap(bitmap,0,0,paint);
        // y = Asin(wx+b)+h ，这个公式里：w影响周期，A影响振幅，h影响y位置，b为初相；
        // 将周期定为view总宽度
        float mCycleFactorW = (float) (2 * Math.PI / len);
        // 得到第一条波的y值
        for (int i = 0; i < len; i++) {
            //添加一个可变的初相值
            firstWaterLine[i] = (float) (20 * Math
                    .sin(mCycleFactorW * i + move) - up);
        }
        // 得到第二条波的y值
        for (int i = 0; i < len; i++) {
            //添加一个可变的初相值
            secondWaterLine[i] = (float) (28 * Math.sin(mCycleFactorW * i + move + 10) - up);

        }
        // 裁剪成圆形区域
        Path path = new Path();
        path.reset();
        float clipRadius = len / 2;
        //添加圆形路径
        //Path.Direction.CCW逆时针
        //Path.Direction.CW顺时针

        path.addCircle(len / 2, len / 2, clipRadius, Path.Direction.CCW);
        // (剪裁路径)裁剪成圆形区域
        //(REPLACE用当前要剪切的区域代替画布中的内容的区域)
        paint.setColor(APP.getAppResources().getColor(R.color.money));
        paint.setStyle(Style.FILL);
        canvas.drawPath(path,paint);
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.STROKE);
        canvas.clipPath(path, android.graphics.Region.Op.REPLACE);

        canvas.save();
        canvas.translate(0, len);

        //第一条波的所有直线
        for (int i = 0; i < len; i++) {
            canvas.drawLine(i, firstWaterLine[i], i, len, paint);
        }
        //第二条波的所有直线
        for (int i = 0; i < len; i++) {
            canvas.drawLine(i, secondWaterLine[i], i, len, paint);
        }
        canvas.restore();
//
    }
    public void moveWaterLine() {
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                //不断改变初相
                move += 1;
                //重新绘制(子线程中调用)
                postInvalidate();
            }
        }, 500, 200);
    }
    public void change(double persent) {
        if (isRunning) {
            return;
        }
        final int trueAngle = (int) (len*persent);
        if (up < trueAngle){
            state = 2;
        }else {
            state = 1;
        }
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                switch (state) {
                    case 1:
                        //isRunning = true;
                        up -= 10;
//                        if (up <= 0) {
//                            up = 0;
//                            state = 2;
//                        }
                        if (up <= trueAngle) {
                            up = trueAngle;
                            timer.cancel();
                        }
                        break;
                    case 2:
                        up += 10;
                        if (up >= trueAngle) {
                            up = trueAngle;
                            //state = 1;
                            //isRunning = false;
                            timer.cancel();
                        }
                        break;
                    default:
                        break;
                }
               /* up += 10;
                if (up >= trueAngle) {
                    up = trueAngle;
                    state = 1;
                    isRunning = false;
                    timer.cancel();
                }*/
                postInvalidate();
            }
        }, 300, 30);

    }

}
